package com.hdcx.Minfresh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.util.RecyclerViewStateUtils;
import com.github.jdsjlzx.view.LoadingFooter;
import com.google.gson.Gson;
import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.adapter.CollectAdapter;
import com.hdcx.Minfresh.bean.CollectWrapper;
import com.hdcx.Minfresh.bean.GoodListWrapper;
import com.hdcx.Minfresh.url.MyUrl;
import com.hdcx.Minfresh.util.AppUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/11/18.
 */

public class HistoryActivity extends BaseActivity {
    CollectAdapter adapter;
    LRecyclerView recyclerView ;
    LRecyclerViewAdapter mLRecyclerViewAdapter;
    Boolean isRefresh = false;
    int num = 1;
    int totleNum;
    CollectWrapper data;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect_);
        initView();
        initData();
    }

    private void initView() {
        recyclerView = (LRecyclerView) findViewById(R.id.recycler_view);
        adapter=new CollectAdapter(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        recyclerView.setAdapter(mLRecyclerViewAdapter);
        ((TextView)findViewById(R.id.tv_title)).setText("浏览记录");
        findViewById(R.id.tv_left).setOnClickListener(this);
        setRecycler();
    }
    private void initData() {
         post_main();
    }
    private void setRecycler() {
        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                num = 1;
                isRefresh = true;
                post_main();
            }
        });
        recyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                if (num * 10 < totleNum) {
                    // loading more
                    num =num+1;
                    RecyclerViewStateUtils.setFooterViewState(HistoryActivity.this, recyclerView, 10, LoadingFooter.State.Loading, null);
                    post_main();
                } else {
                    //the end
                    RecyclerViewStateUtils.setFooterViewState(HistoryActivity.this, recyclerView, 10, LoadingFooter.State.TheEnd, null);

                }
            }
        });
    }
    private void post_main() {
        JSONObject json = new JSONObject();
        try {
            json.put("uid", AppUtil.getUserId());
            json.put("page",num);
            json.put("size","10");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkHttpUtils.postString()
                .url(MyUrl.HISTORY)
                .content(json.toString())
                .build()
                .execute(new Callback<CollectWrapper>() {
                    @Override
                    public CollectWrapper parseNetworkResponse(Response response) throws IOException {
                        String json = response.body().string();
                        return new Gson().fromJson(json, CollectWrapper.class);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                    }

                    @Override
                    public void onResponse(CollectWrapper response) {
                        if (response.getStatus() == 1) {
                            if(num==1){
                                data = response;
                                adapter.setData(response,1);
                            }else{
                                data.getList().addAll(response.getList());
                                adapter.setData(data,1);
                            }
                            if (isRefresh) {
                                recyclerView.refreshComplete();
                            }

                        }
                    }
                });
    }

    @Override
    public void onMyItemClick(Object data) {
        Intent intent = new Intent(this,DetailActivity.class);
        intent.putExtra("id",(String)data);
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_left:
                finish();
                break;
        }
    }
}
